package top.hzy520.order.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.hzy520.common.constants.Constants;
import top.hzy520.common.resp.ApiResult;
import top.hzy520.common.rest.BaseController;
import top.hzy520.order.dto.OrderDto;
import top.hzy520.order.dto.OrderPageDto;
import top.hzy520.order.entity.Order;
import top.hzy520.order.entity.OrderItem;
import top.hzy520.order.entity.PageQueryBean;
import top.hzy520.order.service.OrderService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: HouZeYu
 * @Description: 订单控制层
 * @Date: Created in 10:54 2018/6/10
 */
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {
    @Autowired
    private OrderService orderService;
    /**
    *@Author: HouZeYu
    *@Description: 创建订单
    *@Date: 11:13 2018/6/10
    */
    @RequestMapping("/createOrder")
    public ApiResult createOrder(@RequestBody List<OrderItem> orderItemList, HttpServletRequest request){
        ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"订单创建成功");
        OrderDto order=orderService.createOrder(orderItemList,getCurrentUser(request).getUuid());
        apiResult.setData(order);
        return apiResult;
    }
    /**
    *@Author: HouZeYu
    *@Description: 查询用户订单
    *@Date: 13:57 2018/7/3
    */
    @RequestMapping("/vieworder")
    public ApiResult viewOrder(@RequestBody PageQueryBean pageQueryBean, HttpServletRequest request){
        ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"订单查询成功");
        OrderPageDto order=orderService.viewOrder(pageQueryBean,getCurrentUser(request).getUuid());
        apiResult.setData(order);
        return apiResult;
    }
    /**
    *@Author: HouZeYu
    *@Description: 根据订单编号查询订单Subject
    *@Date: 10:01 2018/9/16
    */
    @RequestMapping("/query")
    public String queryOrderSubject(@RequestBody Order order){
      Order dbOrder=orderService.queryOrderSubjectById(order.getOrderNo());
      if (dbOrder==null){
          return null;
      }else {
          return dbOrder.getOrderSubject();
      }
    }

    /**
    *@Author: HouZeYu
    *@Description: 订单支付成功修改订单状态关闭订单
    *@Date: 12:07 2018/9/16
    */
    @RequestMapping("/verifyorderstatus")
    public void verifyOrderStatus(@RequestBody Order order){
       orderService.verifyOrderStatus(order);
    }
}
